package com.ywu.application.standard.adminUser.api;

import com.ywu.application.standard.adminUser.dto.UserDto;
import com.ywu.application.standard.adminUser.entity.UserEntity;
import com.ywu.application.standard.adminUser.service.UserService;
import com.ywu.common.result.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.Size;
import java.util.List;

/**
 * @ClassName UserApi
 * @Description TODO
 * @Author GroundDemo
 * @Date 2024/3/9 13:44
 * @Version 1.0
 **/
@RestController
@RequestMapping("/ywu/admin/user")
public class UserApi {
    @Autowired
    private UserService userService;

    /**
    * @Author GroundDemo
    * @Description 生成登录验证码
    * @Date 19:31 2024/3/10
    * @Param []
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @GetMapping("/login.verify.code")
    public CommonResult<Object> loginVerifyCode(HttpServletResponse response) {
        // 调用verify-code服务生成验证码
        return userService.loginVerifyCode(response);
    }

    /**
     * @Author GroundDemo
     * @Description 登录动作分为两步，第一步先校验用户名和密码，调用成功返回当前用户下的所有租户
     * @Date 21:08 2024/5/30
     * @Param [userDto, verifyKey, response]
     * @return com.ywu.common.result.CommonResult<java.lang.Object>
     **/
    @PostMapping("/login.user.info.before.check")
    public CommonResult<Object> loginUserBeforeCheck(@RequestBody UserDto userDto, @CookieValue("verifyKey") String verifyKey,
                                                     HttpServletResponse response) {
        return userService.loginUserBeforeCheck(userDto, verifyKey, response);
    }

    /**
    * @Author GroundDemo
    * @Description 登录
    * @Date 17:19 2024/3/10
    * @Param [userDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @PostMapping("/login.user.info")
    public CommonResult<Object> loginUser(@RequestBody UserDto userDto, HttpServletResponse response) {
        return userService.loginUser(userDto, response);
    }

    /**
    * @Author GroundDemo
    * @Description 用户登出
    * @Date 15:18 2024/6/2
    * @Param [userDto, response]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @PostMapping("/login.user.out.info")
    public CommonResult<Object> loginOut(@RequestBody UserDto userDto, HttpServletResponse response) {
        return userService.loginOut(userDto, response);
    }

    /**
    * @Author GroundDemo
    * @Description 创建用户信息
    * @Date 13:45 2024/3/9
    * @Param [userDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @PostMapping("/create.user.info")
    public CommonResult<Object> createUserInfo(@RequestBody @Valid UserDto userDto) {
        return userService.createUserInfo(userDto);
    }

    /**
    * @Author GroundDemo
    * @Description 修改用户信息
    * @Date 14:02 2024/3/9
    * @Param [userDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @PostMapping("/update.user.info")
    public CommonResult<Object> updateUserInfo(@RequestBody @Valid UserDto userDto) {
        return userService.updateUserInfo(userDto);
    }

    /**
    * @Author GroundDemo
    * @Description 删除用户信息
    * @Date 14:09 2024/3/9
    * @Param [userDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @PostMapping("/delete.user.info")
    public CommonResult<Object> deleteUserInfo(@RequestBody UserDto userDto) {
        return userService.deleteUserInfo(userDto);
    }

    /**
    * @Author GroundDemo
    * @Description 查询用户信息列表
    * @Date 14:12 2024/3/9
    * @Param [userDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @PostMapping("/query.user.info")
    public CommonResult<Object> queryUserInfo(@RequestBody UserDto userDto) {
        return userService.queryUserInfo(userDto);
    }

    /**
    * @Author GroundDemo
    * @Description 根据用户账号查询用户信息
    * @Date 16:45 2024/3/10
    * @Param [userDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @PostMapping("/query.user.account.info")
    public CommonResult<UserEntity> queryUserByUserAccount(@RequestBody UserDto userDto) {
        return userService.queryUserByUserAccount(userDto);
    }

    /**
    * @Author GroundDemo
    * @Description 根据ID批量查询用户信息
    * @Date 19:10 2024/5/19
    * @Param []
    * @return com.ywu.common.result.CommonResult<java.util.List<com.ywu.admin.user.standard.entity.UserEntity>>
    **/
    @PostMapping("/query.user.by.id.batch.spi")
    public CommonResult<List<UserEntity>> queryUserByUserIdBatch(@RequestBody @Valid @Size(max = 500) List<Integer> ids) {
        return userService.queryUserByUserIdBatch(ids);
    }

    /**
    * @Author GroundDemo
    * @Description 查询用户的详情，包括用户信息、角色信息、租户信息
    * @Date 14:39 2024/6/2
    * @Param [userDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @PostMapping("/query.user.detail.info")
    public CommonResult<Object> queryUserDetailInfo() {
        return userService.queryUserDetailInfo();
    }

    /**
    * @Author GroundDemo
    * @Description 用户租户修改
    * @Date 16:56 2024/6/2
    * @Param [userDto, response]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    @PostMapping("/user.tenant.change.info")
    public CommonResult<Object> userTenantChange(@RequestBody UserDto userDto, HttpServletResponse response) {
        return userService.userTenantChange(userDto, response);
    }
}
